<?php
/**
 * Created by PhpStorm.
 * User: hey-Dq
 * Date: 2020/6/15
 * Time: 22:43
 */

$redis = new \Redis();
$redis->connect('127.0.0.1','6379',7);
$redis->select(7);

//用户签到
//每个月保留一次
/*$uid = 1;
$date_format = date('Y-m-d', time());
//$sign_key    = $date_format . ':' . $uid;
$sign_key    = sprintf("%s:%d", $date_format,$uid);
$current_month_time = strtotime(date('Y-m-01'));
$offset      = floor((strtotime($date_format) - $current_month_time) / 86400);*/


//签到
/*if ($redis->getBit($sign_key, $offset)) {
    echo '已经签到';
} else {
    $res = $redis->setBit($sign_key, $offset, 1);
    if ($res === 0) {
        echo '签到成功';
    } else {
        echo '签到失败';
    }
}*/

//统计签到次数
/*$sign_count = $redis->bitCount($sign_key);
echo $sign_count;*/

//统计连续签到次数
//最后签到时间
//如何计算本月的连续签到次数
/*$list = '00100111';

$count = 0;
for ($i = 0; $i <= $offset; $i++ ) {

}*/






/*$uid = 1;
$cache_key = sprintf("sign_%d", $uid);
//开始日期
$startDate = '2020-01-01';
$todayDate = '2020-01-21';

$startTime = strtotime($startDate);
$todayTime = strtotime($todayDate);
$offset    = floor(($todayTime - $startTime) / 86400);*/



//签到
/*if ($redis->setBit($cache_key, $offset, 1)) {
    echo "签到成功";
}*/

/*$status = $redis->getBit($cache_key,$offset);
echo 1 == $status ? '已签到' : '还没签到';*/

//计算总的签到次数
//echo $redis->bitCount($cache_key);

//统计日活
/*$user_id = 17;
$key = date('Y-m-d' , time());
$redis->setBit($key, $user_id, 1);
var_dump($redis->bitCount($key));
var_dump($redis->getBit($key, $user_id));*/

/*for ($i=0; $i < 10000; $i++) {
	$redis->pfadd("codehole_5", array('user_' . $i));
}
var_dump($redis->pfCount('codehole_5'));*/